﻿using Business.Core.Annotations;
using Common;
using DataModel;
using LinqToDB;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ProjectManager.User;
using ProjectManager.User.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManagerSys
{
    /// <summary>
    /// 任务评论回复
    /// </summary>
    public class TaskCommentMerber : Common.BusinessBase
    {
        [Doc("新增任务评论", Group = "任务评论")]
        public virtual async Task<dynamic> AddTaskComment(string arg)
        {
            var flag = 0;
            string message = "";
            try
            {
                MANAGER_TASK_COMMENT oModel = JsonConvert.DeserializeObject<MANAGER_TASK_COMMENT>(arg);
                AuthInfo authinfo = Logins.Authorize();
                oModel.ACTIVE = 1;
                oModel.CREATENAME = authinfo.userid;
                oModel.CREATETIME = DateTime.Now;
                oModel.DEPARTCODE = authinfo.departid;
                oModel.REALNAME = authinfo.realname;
                oModel.ID = Guid.NewGuid().ToString();
                oModel.TABLENAME = "MANAGER_TASK";
                using (var con = Data.DB.GetConnection())
                {
                    flag = await con.InsertAsync(oModel);
                    if(flag >= 1) message = "添加成功";
                }
                    
                //审批信息入库
                //string title = "任务标题为：" + oTask.TASKNAME + "的任务已提交，请审批";
                //new LogDataOperation().noticeLog(1, oTask.APPROVALUSER, oTask.ID, title, "MANAGER_TASK");

            }
            catch (Exception ex)
            {
                message = "添加失败，请联系技术人员";
            }
            var obj = new { flag = flag, message = message };
            //入库的用户
            return JsonConvert.SerializeObject(obj);
        }

        [Doc("获取任务评论列表", Group = "任务评论")]
        public virtual async Task<dynamic> GetCommentByID(string taskid)
        {
            List<MANAGER_TASK_COMMENT> list = new List<MANAGER_TASK_COMMENT>();
            try
            {
                using (var con = Data.DB.GetConnection())
                {
                    list = con.MANAGER_TASK_COMMENT.Where(c => c.ACTIVE == 1 && c.RESOURCEID == taskid && c.TABLENAME == "MANAGER_TASK").ToList();
                }
            }
            catch (Exception ex) { }
            
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            return JsonConvert.SerializeObject(list, Formatting.Indented, timeFormat);
        }

        [Doc("获取任务评论内容", Group = "任务评论")]
        public virtual async Task<dynamic> GetCommentDetailByID(string id)
        {
            MANAGER_TASK_COMMENT oModel = new MANAGER_TASK_COMMENT();
            List<MANAGER_TASK_COMMENT> list = new List<MANAGER_TASK_COMMENT>();
            try
            {
                using (var con = Data.DB.GetConnection())
                {
                    oModel = con.MANAGER_TASK_COMMENT.Where(c => c.ID == id).FirstOrDefault();
                    if (oModel != null)
                    {
                        //查询回复列表
                        list = con.MANAGER_TASK_COMMENT.Where(c=>c.TYPE==2 && c.COMMENTID == oModel.ID).ToList();
                    }
                }
            }
            catch (Exception ex) { }
            var obj = new { oModel = oModel, list = list };
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            return JsonConvert.SerializeObject(obj, Formatting.Indented, timeFormat);
        }
    }
}
